import HeadPrefab from "../../component/HeadPrefab";
import { LocationHelper } from "../../engine/LocationHelper";
import { Logger } from "../../engine/Logger";
import ViewBase from "../../engine/ViewBase";
import { RoomModel } from "../model/RoomModel";
import { RoomPlayer } from "../model/RoomPlayer";


const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewRoomLocation extends ViewBase {


    onEnable(): void {
        super.onEnable()

        let playerNum: number = RoomModel.instance.playerList.length
        for (let i = 2; i <= 5; i++) {
            let playerNode: cc.Node = this.findChild(this.node, "playerNode" + i)
            if (i == playerNum) {
                playerNode.active = true
                this.refresh(playerNode)
            }
            else {
                playerNode.active = false
            }
        }
    }

    private refresh(playerNode: cc.Node) {
        for (let i = 0; i < RoomModel.instance.playerList.length; i++) {
            let player: RoomPlayer = RoomModel.instance.playerList[i]
            let playerPrefabNode: cc.Node = this.findChild(playerNode, "player" + i)
            let head: HeadPrefab = this.getChild(playerPrefabNode, "head", HeadPrefab)
            head.setHead(player.headimg, player.lv)
            let iconLocation: cc.Node = this.findChild(playerPrefabNode, "iconLocation")
            if (player.location) {
                iconLocation.active = false
            } else {
                iconLocation.active = true
            }
        }

        for (let i = 0; i < RoomModel.instance.playerList.length - 1; i++) {
            let playerA: RoomPlayer = RoomModel.instance.playerList[i]
            for (let j = i + 1; j < RoomModel.instance.playerList.length; j++) {
                let playerB: RoomPlayer = RoomModel.instance.playerList[j]
                Logger.log("ViewRoomLocation==", i, j, playerA.nick_name, playerB.nick_name)
                let txtDistance: cc.Label = this.getChild(playerNode, "txtDistance" + i + "_" + j, cc.Label)
                let distance: number = LocationHelper.JingWei2Distance(playerA.location, playerB.location)
                txtDistance.string = distance + "米"
            }
        }
    }

}
